use strict;
use warnings;


sub get_new_filename {
    my ($new_filename, $xway_increment, $group_index) = @_;
    my $name_change = ($xway_increment * $group_index);
    
    if ($new_filename =~ m/cardatapoints\.(\d+)\.(.+)/) {
        my $old_number = $1;
        my $new_number = $old_number + $name_change;
        $new_filename =~ s/$old_number/$new_number/;

    } elsif ($new_filename =~ m/cardatapoints\.(\w+)\.(.+)/) {
        my $old_name = $1;
        my $new_name = $old_name . ".$name_change";
        $new_filename =~ s/$old_name/$new_name/;
    }

    return $new_filename;
}


sub generate_new_input_file {
    my ($FILE, $NEW_FILE, $group_index, 
        $xway_increment, $vid_increment, $qid_increment) = @_;

    my @change_indexes = (4, 2, 9);
    my @input_increments = ($xway_increment, $vid_increment, $qid_increment);

    while (my $line = <$FILE>) {
        my @fields = split(/,/, $line);
        for (my $i = 0; $i < @change_indexes; $i++) {
            my $change_index = $change_indexes[$i];
            my $increment = $input_increments[$i];
            my $type = $fields[0];

            if ($type == 3 && $change_index == 4) { 
                next; 
            }

            if ($fields[$change_index] != -1) {
                $fields[$change_index] += ($increment * $group_index);
            }
        }

        my $new_line=join(",",@fields);
        print $NEW_FILE $new_line;
    }
}


sub generate_new_output_file {
    my ($FILE, $NEW_FILE, $group_index, 
        $xway_increment, $vid_increment, $qid_increment) = @_;

    my @type_based_indexes = (1, 3, 3, 3);
    my @output_increments = ($xway_increment, $vid_increment, 
                             $qid_increment, $qid_increment);

    while (my $line = <$FILE>) {
        my @fields=split(/,/, $line);
        my $type = $fields[0];
        my $index = $type_based_indexes[$type];
        my $increment = $output_increments[$index];

        $fields[$index] += ($increment * $group_index);
        my $new_line = join(",", @fields);
        print $NEW_FILE $new_line;
    }
}


sub main {
    my ($filename, $group_index) = @_;

    die "Bad arguments" unless (defined($filename) && defined($group_index));
    if ( ($filename !~ m/cardatapoints\./) || ($group_index <= 0) ) {
        return;  # Nothing to do
    }

    # Increment xway, vid, and qid values according to the specified values.
    my $xway_increment = 10;
    my (@increment_constants) = ($xway_increment, 2000000, 2000000);
    my $new_filename = get_new_filename($filename, $xway_increment, $group_index);

    open(my $FILE, "< $filename") or die $!;
    open(my $NEW_FILE, "> $new_filename") or die $!;

    if ($filename =~ m/cardatapoints\.(.+)\.in/) {
        # If file contains input suffix, create new input file.
        generate_new_input_file($FILE, $NEW_FILE, $group_index, @increment_constants);

    } elsif ($filename =~ m/cardatapoints\.(.+)\.out/) {
        # If file contains output suffix, create new output file.
        generate_new_output_file($FILE, $NEW_FILE, $group_index, @increment_constants);
    }

    # Print the new filename. This filename is passed into the startup shell
    # script, which then sorts the created output file.
    print $new_filename;
}


main(@ARGV);
